package com.calf.cn.utils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class ShiyuanSmsCodeUtil {

	private static final String url = PropertiesUtil.get("sms.url");

	private static final String account = PropertiesUtil.get("sms.account");

	private static final String password = PropertiesUtil.get("sms.password");

	public static final String DEF_CHATSET = "UTF-8";

	public static final int DEF_CONN_TIMEOUT = 30000;

	public static final int DEF_READ_TIMEOUT = 30000;

	public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";

	// 2.发送短信

	public static String getSms(String mobile, String content) {
		String result = "";
		Map<String, Object> params = new HashMap<String, Object>();// 请求参数
		params.put("account", account); // 用户产品账号 短信 语音 国际 不同验证码 换不同账号即可
		params.put("pswd", password);// 用户产品密码
		params.put("mobile", mobile); // 手机号码 多手机号用逗号分隔如
		params.put("msg", content);// 必填参数。短信内容(内容后面必须要带上签名如:[示远科技])
		params.put("needstatus", "true");// 是否需要状态报告，取值true或false，true，表明需要状态报告；false不需要状态报告
		params.put("product","");// 可选参数。用户订购的产品id，不填写（针对老用户）系统采用用户的默认产品，
		try {
			result = net(url, params, "POST");
			result = result.split(",")[1].substring(0, 3);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void main(String[] args) {
		getSms("17717033125", "您的验证码是{code}，请在10分钟内完成操作。为了您的账户安全，请勿泄露给他人。");
	}

	/**
	 *
	 * 
	 * 
	 * @param strUrl
	 *            请求地址
	 * 
	 * @param params
	 *            请求参数
	 * 
	 * @param method
	 *            请求方法
	 * 
	 * @return 网络请求字符串
	 * 
	 * @throws Exception
	 * 
	 */

	public static String net(String strUrl, Map<String, Object> params, String method) throws Exception {
		HttpURLConnection conn = null;
		BufferedReader reader = null;
		String rs = null;
		try {
			StringBuffer sb = new StringBuffer();
			if (method == null || method.equals("GET")) {
				strUrl = strUrl + "?" + urlencode(params);
			}
			URL url = new URL(strUrl);
			conn = (HttpURLConnection) url.openConnection();
			if (method == null || method.equals("GET")) {
				conn.setRequestMethod("GET");
			} else {
				conn.setRequestMethod("POST");
				conn.setDoOutput(true);
			}
			conn.setRequestProperty("User-agent", userAgent);
			conn.setUseCaches(false);
			conn.setConnectTimeout(DEF_CONN_TIMEOUT);
			conn.setReadTimeout(DEF_READ_TIMEOUT);
			conn.setInstanceFollowRedirects(false);
			conn.connect();
			DataOutputStream out = null;
			if (params != null && method.equals("POST")) {
				try {
					out = new DataOutputStream(conn.getOutputStream());
					out.writeBytes(urlencode(params));
				} catch (Exception e) {
				}finally
				{
					out.close();
				}
			}
			InputStream is = conn.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
			String strRead = null;
			while ((strRead = reader.readLine()) != null) {
				sb.append(strRead);
			}
			rs = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				reader.close();
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
		return rs;
	}

	// 将map型转为请求参数型
	public static String urlencode(Map<String, Object> data) {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, Object> i : data.entrySet()) {
			try {
				sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}
